/*
============================================================================
 Name        : BlockScrollInfo.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CBlockScrollInfo implementation
============================================================================
*/

#include <eStock_L2HY.rsg>
#include "eStockappui.h"
#include "eStockTransactionContainer.h"
#include "BaseCommon.h"

#include "TradeScrollInfo.h"

const TInt KPeriodicTimerInterval500 = 100000;
CTradeScrollInfo::CTradeScrollInfo() : CBaseBlock(NULL)
{
	m_pRollInfo = NULL;
	m_bLoading = EFalse;
}

CTradeScrollInfo::~CTradeScrollInfo()
{
	if(NULL != m_pRollInfo)
	{
		m_pRollInfo->Reset();
		delete m_pRollInfo;
	}
	m_pRollInfo = NULL;
	if(m_AnimationTimer)
	{
		delete m_AnimationTimer;
		m_AnimationTimer = NULL;
	}

	CEikonEnv::Static()->ScreenDevice()->ReleaseFont(iFont);
}

CTradeScrollInfo* CTradeScrollInfo::NewLC()
{
	CTradeScrollInfo* self = new (ELeave)CTradeScrollInfo();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

CTradeScrollInfo* CTradeScrollInfo::NewL()
{
	CTradeScrollInfo* self=CTradeScrollInfo::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

void CTradeScrollInfo::ConstructL()
{
	CBaseBlock::ConstructL();
	m_nInfoRollTime = 0;

	m_rect.SetRect(TPoint(0, m_nScreenHeight - KCbabarHeight - KScrollInfobarHeight), TSize(m_nScreenWidth, KScrollInfobarHeight));

	CGlobal::GetDesFromID(m_strDefaultRoll,LOC_BUF_STR_DEFAULTROLL);
	m_strDraw.Copy(m_strDefaultRoll);


	m_nYOffset = 0;
	m_nXOffset = 0;
	m_nCurIndex = 0;

	m_AnimationTimer = CPeriodic::NewL(CActive::EPriorityIdle);

	CGraphicsDevice* screenDevice = CEikonEnv::Static()->ScreenDevice();
	TFontSpec myFontSpec = CEikonEnv::Static()->NormalFont()->FontSpecInTwips();
	myFontSpec.iHeight = screenDevice->VerticalPixelsToTwips(m_rect.Height() - 6);
	screenDevice->GetNearestFontInTwips(iFont, myFontSpec);
}

void CTradeScrollInfo::ShowScrollInfo(TBool aShow)
{
	m_pGlobal->iShowScrollInfo = aShow;
}

void CTradeScrollInfo::DrawOneSecond(CFbsBitGc& g)
{
	if (!m_pGlobal->iShowScrollInfo)
		return;
	TRect rect = GetRect();
	g.SetClippingRect(rect);

	g.SetBrushColor(TRgb(41, 46, 49));
	g.SetBrushStyle(CGraphicsContext::ESolidBrush);
	g.SetPenStyle(CGraphicsContext::ENullPen);
	g.DrawRect(rect);
	
	if (iDownPercentage > 0)
	{
		TInt w = iDownPercentage * rect.Width() / 100;
		TRect percentRect(rect.iTl, TPoint(rect.iTl.iX + w, rect.iBr.iY));
		g.SetBrushColor(TRgb(0x37,0x5B,0x95));
		g.DrawRect(percentRect);
	}
	g.CancelClippingRect();
}

void CTradeScrollInfo::OnDraw(CFbsBitGc& g)
{
	if(m_bLoading)
		DrawOneSecond(g);
	
	if (!m_pGlobal->iShowScrollInfo)
		return;
		
	g.SetClippingRect(m_rect);
	TInt y = m_rect.iBr.iY-m_nYOffset;
	TInt nWidthChar = iFont->TextWidthInPixels(m_strDraw);
	if(m_nYOffset>m_nLineHeight || nWidthChar <= m_rect.Width())
		y =  m_rect.iTl.iY;

	if(!m_bLoading)
	{
		g.SetPenColor(KRgbBlack);
		g.SetBrushColor(KRgbBlack);
		g.SetBrushStyle(CGraphicsContext::ESolidBrush);
		g.DrawRect(m_rect);
	}
	
	g.UseFont(iFont);
	g.SetPenStyle(CGraphicsContext::ESolidPen);
	g.SetBrushStyle(CGraphicsContext::ENullBrush);
	g.SetPenColor(KRgbScrollInfoText);
	g.DrawText(m_strDraw, TPoint(m_rect.iTl.iX - m_nXOffset, y + iFont->HeightInPixels() + 1));
	g.DiscardFont();
	
	g.CancelClippingRect();
}

void CTradeScrollInfo::SizeChanged()
{
	if (!m_pGlobal->iShowScrollInfo)
	{
		m_rect.SetRect(0, 0, 0, 0);
		return;
	}
	if (m_pGlobal->g_nMobileOS == 5 && m_pGlobal->iWideScreen)
	    m_rect.SetRect(TPoint(0, m_nScreenHeight - KTabbarHeight - KScrollInfobarHeight), TSize(m_nScreenWidth, KScrollInfobarHeight));
	else
	    m_rect.SetRect(TPoint(0, m_nScreenHeight - KCbabarHeight - KScrollInfobarHeight), TSize(m_nScreenWidth, KScrollInfobarHeight));

	if (m_nYOffset>m_nLineHeight)
		m_nYOffset = m_nLineHeight;
}

TInt CTradeScrollInfo::OnTitleRoll(TAny* aAny)
{
	CTradeScrollInfo* self = static_cast<CTradeScrollInfo*>( aAny );
	self->UpdatePosition();
	return KErrNone;
}

void CTradeScrollInfo::UpdatePosition()
{	
	TInt nWidthChar = iFont->TextWidthInPixels(m_strDraw);
	if (m_nYOffset < m_rect.Height())
	{
		m_nYOffset += 2;
		if (nWidthChar < m_rect.Width())
		    return;
	}
	else
		m_nXOffset += 2;
	
	if(m_nXOffset > (nWidthChar - m_rect.Width()))
	{
		m_nYOffset = 0;
		m_nXOffset = 0;
		m_nCurIndex ++;
		if(m_pRollInfo && m_nCurIndex >= m_pRollInfo->Count() )
			m_nCurIndex = 0;

		if(m_pRollInfo && m_pRollInfo->Count() > 0)
		{
			m_strDraw.Copy(m_pRollInfo->MdcaPoint(m_nCurIndex));
		}
		else
			m_strDraw.Copy(m_strDefaultRoll);
	}

	DrawNow(m_rect);
}


void CTradeScrollInfo::StartScroll()
{
	m_AnimationTimer->Cancel();
	m_AnimationTimer->Start(
		KPeriodicTimerInterval500, KPeriodicTimerInterval500, 
		TCallBack(OnTitleRoll, this));
}

void CTradeScrollInfo::AppendDesC(const TDesC& buf)
{
	if(NULL == m_pRollInfo)
		m_pRollInfo = new(ELeave)CDesC16ArrayFlat(10);
	
	if(buf.Length())
		m_pRollInfo->AppendL(buf);
}
